//
//  STLPopup_window.m
//  哈哈
//
//  Created by 孙健 on 16/3/31.
//  Copyright © 2016年 孙健. All rights reserved.
//

#import "STLPopup_window.h"

#define Width [UIScreen mainScreen].bounds.size.width
#define Height [UIScreen mainScreen].bounds.size.height

@interface STLPopup_window ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIView *viewTitle;
@property (nonatomic, strong) NSArray *dataSource;

@end

@implementation STLPopup_window

- (instancetype)initWithFrame:(CGRect)frame with:(NSArray *)arrayTitle {
    
    self = [super initWithFrame:frame];
    if (self) {
        self.dataSource = [NSArray array];
        self.dataSource = arrayTitle;
        
       // UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hiddenView)];
        
        //[self addGestureRecognizer:tap];
        [self makeViewWithArray:arrayTitle];
    }
    
    return self;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    [self hiddenView];
}

- (void)makeViewWithArray:(NSArray *)titleArray {
    
    self.viewTitle = [[UIView alloc] initWithFrame:CGRectMake(10, Height, Width - 20, titleArray.count * 50 + 120)];
    self.viewTitle.layer.cornerRadius = 20;
    self.viewTitle.layer.masksToBounds = YES;
    self.viewTitle.backgroundColor = [UIColor clearColor];
    self.tableView = [[UITableView alloc] initWithFrame:self.viewTitle.bounds style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.backgroundColor = [UIColor clearColor];
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLineEtched;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"haha"];
    
    [self.viewTitle addSubview:self.tableView];
    self.backgroundColor = [UIColor clearColor];
    [self addSubview:self.viewTitle];
    
    [UIView animateWithDuration:0.5 animations:^{
       
        CGRect frame = self.viewTitle.frame;
        frame.origin.y -= self.viewTitle.bounds.size.height;
        self.viewTitle.frame = frame;
        
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        
        return self.dataSource.count;
    }
    else {
        
        return 1;
    }
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"haha"];
    cell.textLabel.textAlignment = NSTextAlignmentCenter;
    cell.textLabel.textColor = [UIColor blueColor];
    cell.selectionStyle = UITableViewCellSelectionStyleBlue;
    
    cell.backgroundColor = [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:0.9];
    if (indexPath.section == 0) {
        
        cell.textLabel.text = self.dataSource[indexPath.row];
        
    }
    
    else {
        
        cell.textLabel.text = @"取消";
    }
    
    return cell;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.section == 0) {
        
        [self.delegate chooseCount:indexPath.row WithTitle:self.dataSource[indexPath.row]];
    }
    
    if (indexPath.section == 1) {
        
        [self hiddenView];
    }
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    if (section == 0) {
        return 0;
    }
    else {
        
        return 5;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 50;
}




- (void)hiddenView {
    
    [UIView animateWithDuration:0.5 animations:^{
       
        CGRect frame = self.viewTitle.frame;
        frame.origin.y += frame.size.height;
        self.viewTitle.frame = frame;
    }];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self removeFromSuperview];
    });
}


@end
